package com.example.hyg_veintype.activity.bleList.adapter;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.base.baseadapter.BaseHolder;
import com.example.hyg_veintype.base.baseadapter.BaseRecyclerAdapter;
import com.example.hyg_veintype.constant.ble.BleRssiDevice;

import java.util.List;


public class BlueToothListAdapter extends BaseRecyclerAdapter<BleRssiDevice> {
    Button btConnect;

    public BlueToothListAdapter(List<BleRssiDevice> infos, Button btConnect, Context context) {
        super(infos, context);
        this.btConnect = btConnect;
    }

    @Override
    public BaseHolder<BleRssiDevice> getHolder(View v, int viewType) {
        return new MyHolder(v, context);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.veintype_item_blue;
    }

    class MyHolder extends BaseHolder<BleRssiDevice> {
        LinearLayout lnCheckStatus;
        TextView tvName;
        TextView tvMac;
        TextView tvRssi;

        public MyHolder(View itemView, Context context) {
            super(itemView, context);
            lnCheckStatus = itemView.findViewById(R.id.ln_check_status);
            tvName = itemView.findViewById(R.id.tv_name);
            tvMac = itemView.findViewById(R.id.tv_mac);
            tvRssi = itemView.findViewById(R.id.tv_rssi);
        }

        @Override
        public void setData(BleRssiDevice data, int position) {
            if (TextUtils.isEmpty(data.getBleName())) {
                tvName.setText("未知设备");
            } else {
                tvName.setText(data.getBleName());
            }
            tvMac.setText(data.getBleAddress());
            int rssi = data.getRssi();
            if (rssi > -100) {
                tvRssi.setText("强");
            } else {
                tvRssi.setText("弱");
            }
            //item选中样式
            if (data.isChecked()) {
                lnCheckStatus.setSelected(true);
            } else {
                lnCheckStatus.setSelected(false);
            }
            //控制连接按钮是否可以点击
            boolean btnEnable = false;
            for (int i = 0; i < mInfos.size(); i++) {
                if (mInfos.get(i).isChecked()) {
                    btnEnable = true;
                    break;
                }
            }
            if (btnEnable) {
                btConnect.setEnabled(true);
                btConnect.setBackground(ContextCompat.getDrawable(context, R.drawable.veintype_circle_purple));
            } else {
                btConnect.setEnabled(false);
                btConnect.setBackground(ContextCompat.getDrawable(context, R.drawable.veintype_circle_gray));
            }

            lnCheckStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (BleRssiDevice mInfo : mInfos) {
                        mInfo.setChecked(false);
                    }
                    data.setChecked(true);
                    notifyDataSetChanged();
                }
            });
        }

    }
}
